<?php

namespace App\WeChat\OAuth\Middleware;

use App\WeChat\Contracts\WeChatOAuth;
use Closure;

class WxAuth
{
    protected $wechatOAuth;
    public function __construct(WeChatOAuth $oauth)
    {
        $this->wechatOAuth = $oauth;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        if(!$this->wechatOAuth->check()) {
            $response = $this->wechatOAuth->authenticate($request);
            if($response)
                return $response;
        }
        

        return $next($request);
    }
}
